Skip to content

feat(opencode): fall back to another model on a content-filter refusal - #74

Merged
goleary merged 3 commits into
devfrom
refusal-fallback
Nov 13, 2026
Merged

goleary merged 3 commits into
devfrom
refusal-fallback

Conversation

@goleary

@goleary goleary commented Nov 13, 2026 •

Copy link
Copy Markdown

Lets a chat that the provider's content filter refuses carry on with another model, using the fallback we already have for provider outages. Off unless the coordinator's fallback config sets fallbackOnRefusal: true.

Symptom

Since 2026-11-09, Sonnet 5.5 (OpenRouter → Anthropic) refuses about 35 replies a day, up from 3 in the nine days before. In 20 of 22 affected chats the first refusal came 15–60 seconds after an auto-compaction, with 0 output tokens. The chat never recovers: every later message is refused, and the customer sees "Something went wrong with this turn." Linear: USE-2845, earlier REPL-35643.

Tested by hand on one stuck production chat on 2026-11-13: Sonnet 5.5 was refused 3 times out of 3, while Opus 5, Opus 5.5, GPT-6.1 Terra and GPT-5.6 Terra each answered the same history. Going back to Sonnet 5.5 afterwards was refused again.

Cause

The fallback only reacts to errors: a failed request, or an error chunk inside a 200 stream. A refusal is a normal stream that ends with the finish reason content-filter. The loop turns that into ContentFilterError after the step is over, outside the retry policy, so the fallback never sees it.

Why the filter fires is not known. OpenRouter forwards native_finish_reason: refusal with no category.

Fix

When a step's stream ends with content-filter and the config opts in, the processor fails the attempt with ContentFilterError after the step's own bookkeeping has run, and the fallback treats that error as a reason to switch. The rest is existing behaviour:

  • No same-model retry. The error is not retryable, so the swap is immediate.
  • Later steps stay on the fallback. The refused model is put on the usual cooldown, and route() starts each step on the model that last worked. When the cooldown ends, the next step tries the original model once, is refused, and swaps again inside the same step.
  • A swap clears the refused attempt's finish. Otherwise a fallback that fails before finishing a step of its own, for example with a context overflow, would be read as a refusal and the loop would stop without compacting.
  • A refusal no route answers is left exactly as today. The processor does not record an error for it. The row keeps finish: "content-filter" and the prompt loop reports ContentFilterError once, on the same path as with the setting off. That covers a refused fallback, a spent swap budget, a missing target, and a step that already ran a tool, which the fallback never swaps because another model could run the tool again.
  • Compaction is excluded. A refused summary step is handled as before.

The refused request's cost stays on the assistant row and on its step-finish part.

fallbackOnRefusal is optional, so this release changes nothing until the coordinator sends it. Turning it off again is a coordinator deploy, not a fleet-wide opencode downgrade.

Trade-offs

  • The cooldown is per model for the whole sandbox, not per chat. One refused chat moves that user's other chats in the same project, subagents included, to the fallback for the cooldown (5 minutes in production).
  • A refusal that is correct also triggers the swap. The fallback model then applies its own safeguards. That is the price of treating the filter's false positives as recoverable.
  • The status line and the [model-fallback] log line read as a provider failure for a refusal too. The two Agent Model Fallback alerts in andytown count that log line, so they need to exclude refusals before this is turned on.

Tests

  • fallback.test.ts: a refusal switches models only when the config opts in.
  • processor-effect.test.ts: a refused step is answered by the fallback with one request each; a fallback that then fails leaves its own error and no refused finish; a refused step that ran a tool is not swapped; a refusal no route answers is left for the loop.
  • prompt.test.ts, through the loop: a refused turn is answered with no error event; a fallback that overflows after a refusal compacts and answers; a refusal on both routes reports one ContentFilterError.

The overflow and tool cases came out of a review of the first version of this PR, where they failed. Ran the processor-effect, fallback, retry and prompt files (163 pass, 0 fail) and tsgo --noEmit on packages/opencode. Not run against a live provider or in a sandbox.

Release

Needs v1.18.32-12 per RELEASING.md. andytown then bumps the three opencode pins; the config that turns this on is replohq/andytown#32481. andytown currently pins 1.18.32-10, so that bump also picks up -11.

🤖 Generated with Claude Code

A refusal ends the stream cleanly, so the fallback never saw it and the
chat stayed stuck on a model that keeps refusing its history. With
fallbackOnRefusal set in the fallback config, a content-filter finish
fails the step and switches models like a provider error.

Co-Authored-By: Claude <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown

The following comment was made by an LLM, it may be inaccurate:

@goleary
goleary marked this pull request as ready for review November 13, 2026 18:59

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Devin Review found 1 potential issue.

Devin Review

Comment on lines +530 to +535
if (value.reason === "content-filter" && SessionFallback.config()?.fallbackOnRefusal)
return yield* Effect.fail(
new SessionV1.ContentFilterError({
message: "The response was blocked by the provider's content filter",
}).toObject(),
)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔴 Fallback overflow aborts compaction

When the fallback overflows after a refusal, ctx.assistantMessage.finish still says content-filter. The prompt loop reports that refusal before handling the requested compaction, leaving the chat stuck.

Learn more

The refused step stores finish: "content-filter" before the new failure enters the retry policy. A successful fallback overwrites it, but a fallback that fails with context overflow before step-finish leaves it in place. halt requests compaction without setting an assistant error. The prompt loop checks the stale finish before processing result === "compact", so it stops on a refusal instead. A fallback API failure before step-finish also inherits the wrong finish reason.

Example: Sonnet refuses a long history; the configured smaller fallback rejects that history as too long. Instead of compacting and retrying, the turn ends with ContentFilterError attributed to Sonnet.

Recommended fix: Clear and persist the refused attempt's finish when the processor commits to a fallback swap, while retaining the refused attempt's cost and step-finish part. Verify both fallback context overflow and fallback errors before step-finish.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 5 files

Tip: Get a Slack DM when cubic finishes reviewing your PRs. Connect Slack

View guided diff | Turn on auto-fix | Re-trigger cubic

…p happens

A swap now clears the refused attempt's finish, so a fallback that fails
before finishing a step is not read as a refusal. A refusal no route
answers is left for the prompt loop to report, as with the setting off,
and compaction is excluded.

Co-Authored-By: Claude <noreply@anthropic.com>
@goleary
goleary merged commit d52cad6 into dev Nov 13, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant